<template>
  <div class="games-create">
    <div class="headline" v-if='gameList.length > 0'>
      <span></span>我创建的比赛
    </div>
    <a class="game" href="javascript:;" v-for="item in gameList" @click.stop.prevent="gameDetail(item)">
      <div class="text-center">
        <img src="~assets/images/shuyezuo@3x.png">
        <div class="games-block-content">
          <div class="game-name" v-text='item.GAME_NAME'></div>
          <div class="date">
            {{item.BEGIN_DATE | formatDatePoint}} - {{item.END_DATE | formatDatePoint}}
            <b>
              <i class="arrow-right1"></i><i class="arrow-right2"></i>
            </b>
          </div>
        </div>
        <img src="~assets/images/shuyeyou@3x.png">
      </div>
      <div class="row">
        <div class="col-xs-6 text-center days-remaining no-padding">
          <div class="key" v-if="item.tips == ''">距离报名截止日</div>
          <div class="value" v-if="item.tips == ''">
            <span class="color-red">{{item.daysRemaining}}</span><span class="unit">天</span>
          </div>
          <div class="tips" v-if="item.tips!= ''">{{item.tips}}</div>
        </div>
        <div class="col-xs-6 text-center head-count no-padding">
          <div class="key">参加人数</div>
          <div class="value">
            <span :class="{'color-red' : parseInt(item.GAME_ATTEND_PERSON) >= parseInt(item.GAME_MAX_LIMIT)}">{{item.GAME_ATTEND_PERSON}}</span>/{{item.GAME_MAX_LIMIT}}
          </div>
        </div>
      </div>
      <div class='forward'>
        <i class='iconfont icon-xiangyou'></i>
      </div>
    </a>
    <div class="placeholder" v-if="gameList.length == 0">
      <div class="content">
        <div class="tips" v-if="error == ''">
          哎呦，我还没有
          <a href="javascript:;" class="color-blue" @click.stop.prevent="buildGame">创建比赛</a>
          哦
        </div>
        <div class="tips" v-else>
          {{error}}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import RemoteDAO from 'common/remoteDAO'
  import CommonUtil from 'common/common.util'
  import LocalDAO from 'common/localDAO'
  import formatDatePoint from 'filters/formatDatePoint'
  import filterGameStatus from 'filters/filterGameStatus'
  import filterGameStep from 'filters/filterGameStep'
  import { openGame } from 'components/game/openGame'
  export default {
    filters: {
      formatDatePoint,
      filterGameStatus
    },
    data () {
      return {
        title: '牛人牛股',
        gameList: [],
//        gameList: [{gameName:'比赛名', rankNum: '0',gameAttendPerson:'0',beginDate:'xxxx.xx.xx',endDate:'xxxx.xx.xx',tips:'',daysRemaining:'3',gameMaxLimit:'20'}],
        error: ''
      }
    },
    created: function () {
      this.fetchData()
    },
    methods: {
      fetchData () {
        let userCode = LocalDAO.getUserCode();
        // 查询我创建的比赛
        RemoteDAO.callGT1008(resp => {
          if (Number(resp.error) === 0) {
            let dateNow = resp.data.body.date
            let params = {
              userCode: userCode,
              actionFlag: '2',
              pageSize: '500',
              pageNo: '1',
              totalFlag: '0',
              gameKind: '',
              gameID: ''
            }
            RemoteDAO.callUC12008303(params, response => {
              if(response.error === '0' ) {
                // this.gameList = response.data.body
                for(let i = 0; i < response.data.body.length; i++){
                  response.data.body[i].tips = filterGameStatus(response.data.body[i].GAME_STATUS)
                  if(response.data.body[i].GAME_STATUS === '1'){
                    response.data.body[i].tips = filterGameStep(response.data.body[i].GAME_STEP)
                  }
                  let startTime = Date.parse(CommonUtil.formatDate(response.data.body[i].JOIN_END_DATE));
                  let endTime = Date.parse(CommonUtil.formatDate(dateNow + '')); //new Date().getTime();

                  let daysRemaining = CommonUtil.dateDiff(startTime, endTime);
                  if (daysRemaining > 0) {
                    response.data.body[i].daysRemaining = Math.ceil(daysRemaining);
                  } else if (daysRemaining == 0) {
                    response.data.body[i].daysRemaining = '今';
                  } else {
                    response.data.body[i].daysRemaining = '--';
                  }
                  this.gameList.push(response.data.body[i]);
                }
                this.error = '';
                // let list = [];
                // for(let i = 0; i < response.data.body.lenght; i++) {
                //   let object = {};
                //   let item = response.data.body[i]

                //   object.gameId = item.GAME_ID;
                //   object.gameName = item.GAME_NAME;
                //   object.beginDate = CommonUtil.formatDatePoint(item.BEGIN_DATE);
                //   object.endDate = CommonUtil.formatDatePoint(item.END_DATE);
                //   object.joinEndDate = item.JOIN_END_DATE;
                //   object.newPlayers = '--';
                //   object.gameAttendPerson = item.GAME_ATTEND_PERSON;
                //   object.gameMaxLimit = item.GAME_MAX_LIMIT;
                //   object.gameStatus = item.GAME_STATUS;
                //   object.gameStep = item.GAME_STEP;

                //   //DELETE为1，比赛直接无效
                //   if (item.DELETE == '1') {
                //     object.gameStep = '5';
                //   }
                  // if(object.gameStatus !=1 && item.END_DATE < dateNow ){
                  //   object.tips = '审核未通过'
                  // }else{
                  //   switch (object.gameStatus) {
                  //     case '0':
                  //       object.tips = '待审核';
                  //       break;
                  //     case '1':
                  //       switch (object.gameStep) {
                  //         case '0':
                  //           object.tips = '尚未开放报名';
                  //           break;
                  //         case '1':
                  //           object.tips = '';
                  //           break;
                  //         case '2':
                  //           object.tips = '尚未开赛';
                  //           break;
                  //         case '3':
                  //           object.tips = '比赛中';
                  //           break;
                  //         case '4':
                  //           object.tips = '已结束';
                  //           break;
                  //         case '5':
                  //           object.tips = '比赛失效';
                  //           break;
                  //         default:
                  //           object.tips = '';
                  //       }

                  //       break;
                  //     case '2':
                  //       object.tips = '审核中';
                  //       break;
                  //     case '3':
                  //       object.tips = '已停赛';
                  //       break;
                  //     case '4':
                  //       object.tips = '已删除';
                  //       break;
                  //     default:
                  //       object.tips = '';
                  //   }
                  // }


                  // list.push(object);
                // }
                // self.gameList = list;
                // self.error = '';
                //  LocalDAO.setGamesCreate(gamesCreate);
              }else{
                this.error = response.data
              }
            });
          } else {
            this.error = resp.data
          }
        });
      },
      buildGame(){
        this.$router.push('/game/buildgame')
      },
      gameDetail (item){
        // this.$router.push({path: '/game/gamedetail', query: {gameId: item.gameId}});
        openGame(item, this)
      }
    }
  }
</script>

<style scoped>

</style>
